package com.yuzhi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.yuzhi.common.core.page.PageDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yuzhi.system.domain.bo.SysUserDeptScopeBo;
import com.yuzhi.system.domain.request.SysUserDeptScopeReq;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.yuzhi.system.domain.vo.SysUserDeptScopeVo;
import com.yuzhi.system.domain.SysUserDeptScope;
import com.yuzhi.system.mapper.SysUserDeptScopeMapper;
import com.yuzhi.system.service.ISysUserDeptScopeService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 用户和部门数据范围关联Service业务层处理
 *
 * @author auto code generator
 * @date 2024-04-07
 */
@RequiredArgsConstructor
@Service
public class SysUserDeptScopeServiceImpl implements ISysUserDeptScopeService {

    private final SysUserDeptScopeMapper baseMapper;

    /**
     * 查询用户和部门数据范围关联
     */
    @Override
    public SysUserDeptScopeVo queryById(String userId){
        return baseMapper.selectVoById(userId);
    }

    /**
     * 查询用户和部门数据范围关联列表
     */
    @Override
    public PageDataInfo<SysUserDeptScopeVo> queryPageList(SysUserDeptScopeReq request) {
        LambdaQueryWrapper<SysUserDeptScope> lqw = buildQueryWrapper(request);
        Page<SysUserDeptScopeVo> result = baseMapper.selectVoPage(request.getPageQuery().build(), lqw);
        return PageDataInfo.build(result);
    }

    /**
     * 查询用户和部门数据范围关联列表
     */
    @Override
    public List<SysUserDeptScopeVo> queryList(SysUserDeptScopeReq request) {
        LambdaQueryWrapper<SysUserDeptScope> lqw = buildQueryWrapper(request);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysUserDeptScope> buildQueryWrapper(SysUserDeptScopeReq request) {
        LambdaQueryWrapper<SysUserDeptScope> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(request.getUserId()),SysUserDeptScope::getUserId,request.getUserId())
                .eq(StrUtil.isNotBlank(request.getDeptId()),SysUserDeptScope::getDeptId,request.getDeptId());
        return lqw;
    }

    /**
     * 新增用户和部门数据范围关联
     */
    @Override
    public Boolean insertByBo(SysUserDeptScopeBo bo) {
        boolean flag = false;
        SysUserDeptScope add = BeanUtil.toBean(bo, SysUserDeptScope.class);
        validEntityBeforeSave(add);
        List<SysUserDeptScopeVo> sysUserDeptScopeVos = queryList(new SysUserDeptScopeReq().setDeptId(bo.getDeptId()).setUserId(bo.getUserId()));
        if (sysUserDeptScopeVos.size() == 0) {
            flag = baseMapper.insert(add) > 0;
            if (flag) {
                bo.setUserId(add.getUserId());
            }
        }
        return flag;
    }

    /**
     * 修改用户和部门数据范围关联
     */
    @Override
    public Boolean updateByBo(SysUserDeptScopeBo bo) {
        SysUserDeptScope update = BeanUtil.toBean(bo, SysUserDeptScope.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysUserDeptScope entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户和部门数据范围关联
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public void deleteByUserId(String userId) {
        this.baseMapper.delete(Wrappers.<SysUserDeptScope>lambdaQuery().eq(SysUserDeptScope::getUserId,userId));
    }
}
